using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace XNATetris
{
    public interface IInputService
    {
        event Action UpOn;
        event Action UpOff;
        event Action DownOn;
        event Action DownOff;
        event Action EnterOn;
        event Action EnterOff;
    }

    public class Input : Microsoft.Xna.Framework.GameComponent, IInputService
    {
        public Input(Game game)
            : base(game)
        {
            if (game.Services.GetService(typeof(IInputService)) == null)
                game.Services.AddService(typeof(IInputService), this);
        }

        public event Action UpOn;
        public event Action UpOff;
        public event Action DownOn;
        public event Action DownOff;
        public event Action EnterOn;
        public event Action EnterOff;

#if XBOX360
        GamePadState oldState;
#endif

#if WINDOWS
        KeyboardState oldState;
#endif

        public override void Initialize()
        {
#if XBOX360
                oldState = GamePad.GetState(PlayerIndex.One);
#endif

#if WINDOWS
                oldState = Keyboard.GetState();
#endif

            base.Initialize();
        }

        
        public override void Update(GameTime gameTime)
        {
#if XBOX360
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadUp) && oldState.IsButtonUp(Buttons.DPadUp))
#endif

#if WINDOWS
            if (Keyboard.GetState().IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up))
#endif
            {
                if (UpOn != null)
                {
                    UpOn();
                }
            }
            else
            {
                if (UpOff != null)
                {
                    UpOff();
                }
            }

#if XBOX360
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadDown) && oldState.IsButtonUp(Buttons.DPadDown))
#endif

#if WINDOWS
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down))
#endif
            {
                if (DownOn != null)
                {
                    DownOn();
                }
            }
            else
            {
                if (DownOff != null)
                {
                    DownOff();
                }
            }

#if XBOX360
            if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) && oldState.IsButtonUp(Buttons.A))
#endif

#if WINDOWS
            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
#endif
            {
                if (EnterOn != null)
                {
                    EnterOn();
                }
            }
            else
            {
                if (EnterOff != null)
                {
                    EnterOff();
                }
            }
#if XBOX360
            oldState = GamePad.GetState(PlayerIndex.One);
#endif

#if WINDOWS
            oldState = Keyboard.GetState();
#endif

            base.Update(gameTime);
        }
    }
}
